package cur.nature.component.websocket.controller;

import com.alibaba.fastjson2.JSONObject;
import cur.nature.component.websocket.config.WebSocketServer;
import cur.nature.framework.core.log.LOG;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 缓存 Controller
 * 作者：Auto Generator By wujian
 * 生成日期：2023-08-05 17:57:55
 */
//  https://blog.csdn.net/weixin_44185837/article/details/124942482
//  https://zhuanlan.zhihu.com/p/613527997?utm_id=0
@Tag(name = "websocket 服务接口")
@RestController
@RequestMapping("component/websocket/v1/api/Component_Server")
@ConditionalOnProperty( value = {"cur.component.websocket.controller.enable"})
public class Websocket_ServerController {

    @Resource
    private WebSocketServer webSocket;

    @Operation(summary = "send 本服务")
    @GetMapping("/send")
    public String ping() throws Exception {
        LOG.info("send : " );
        //创建业务消息信息
        JSONObject obj = new JSONObject();
        obj.put("cmd", "topic");//业务类型
        obj.put("msgId", 1);//消息id
        obj.put("msgTxt", "消息");//消息内容
        //全体发送
        webSocket.sendAllMessage(obj.toJSONString());
        //单个用户发送 (userId为用户id)
        webSocket.sendOneMessage("1", obj.toJSONString());
        //多个用户发送 (userIds为多个用户id，逗号‘,’分隔)
        webSocket.sendMoreMessage(new String[]{"1","2"}, obj.toJSONString());
        return "1";
    }


}

